<template>
  <div class="login-log-container">
    <el-card>
      <template #header>
        <div class="header">
          <div class="search-area">
            <el-input
              v-model="searchQuery"
              placeholder="请输入用户名"
              style="width: 200px"
              clearable
              @clear="handleSearch"
            />
            <el-select v-model="loginStatus" placeholder="登录状态" clearable style="margin-left: 10px">
              <el-option label="全部" value="" />
              <el-option label="成功" value="1" />
              <el-option label="失败" value="0" />
            </el-select>
            <el-date-picker
              v-model="dateRange"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              style="margin-left: 10px"
            />
          </div>
          <el-button type="primary" @click="handleExport">
            <el-icon><Download /></el-icon>
            导出
          </el-button>
        </div>
      </template>

      <el-table :data="tableData" style="width: 100%" v-loading="loading">
        <el-table-column prop="loginTime" label="登录时间" width="180" />
        <el-table-column prop="username" label="用户名" width="120" />
        <el-table-column prop="ip" label="IP地址" width="130" />
        <el-table-column prop="location" label="登录地点" width="150" />
        <el-table-column prop="browser" label="浏览器" width="150" />
        <el-table-column prop="os" label="操作系统" width="150" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="row.status === 1 ? 'success' : 'danger'">
              {{ row.status === 1 ? '成功' : '失败' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="msg" label="操作信息" />
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 30, 50]"
          layout="total, sizes, prev, pager, next"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { Download } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const searchQuery = ref('')
const loginStatus = ref('')
const dateRange = ref([])
const loading = ref(false)
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

const tableData = ref([
  {
    loginTime: '2025-07-18 10:00:00',
    username: 'admin',
    ip: '192.168.1.1',
    location: '北京市',
    browser: 'Chrome 120.0.0.0',
    os: 'Windows 10',
    status: 1,
    msg: '登录成功'
  },
  {
    loginTime: '2025-07-18 09:50:00',
    username: 'test',
    ip: '192.168.1.2',
    location: '上海市',
    browser: 'Firefox 120.0',
    os: 'macOS',
    status: 0,
    msg: '密码错误'
  }
])

const handleSearch = () => {
  // 实现搜索逻辑
}

const handleExport = () => {
  // 实现导出逻辑
  ElMessage.success('导出成功')
}

const handleSizeChange = (val) => {
  pageSize.value = val
  // 重新加载数据
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  // 重新加载数据
}
</script>

<style scoped>
.login-log-container {
  padding: 20px;
}
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.search-area {
  display: flex;
  align-items: center;
}
.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
